package com.xydtech.ecard.busi.controller;

import com.alibaba.fastjson.JSONObject;
import com.xydtech.common.annotation.RsaRequest;
import com.xydtech.common.core.domain.entity.SysDept;
import com.xydtech.common.model.ResponseModel;
import com.xydtech.common.utils.DateUtils;
import com.xydtech.ecard.busi.domain.EcardApiBusiReportConfig;
import com.xydtech.ecard.busi.domain.EcardApiBusiReportPublic;
import com.xydtech.ecard.busi.service.IEcardApiBusiReportConfigService;
import com.xydtech.ecard.busi.service.IEcardApiBusiReportPublicService;
import com.xydtech.ecard.ecard.controller.EcardApiBaseController;
import com.xydtech.ecard.user.service.IEcardApiUserService;
import com.xydtech.system.service.ISysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 电子名片业务填报对公类信息Controller
 *
 * @author yuzhiguo
 * @date 2024-09-26 下午1:13
 */
@Controller
@Slf4j
@Api(tags = "电子名片-业务填报对公类接口")
@RequestMapping("/ecardSaasApi/busi/reportPublic")
public class EcardApiBusiReportPublicController extends EcardApiBaseController {

    @Autowired
    private IEcardApiBusiReportPublicService ecardBusiReportPubliScervice;
    @Autowired
    private IEcardApiBusiReportConfigService ecardBusiReportConfigService;
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private IEcardApiUserService ecardApiUserService;

    /**
     * 电子名片-保存业务填报对公类数据接口
     *
     * @author yuzhiguo
     * @date 2024/9/26 下午1:45
     */
    @PostMapping(value = "/addReportPublicInfo")
    @ApiOperation(value = "电子名片-保存业务填报对公类数据接口", notes = "电子名片-保存业务填报绑卡类数据接口")
    @ResponseBody
    @RsaRequest(result = false)
    public ResponseModel addReportPublicInfo(@RequestBody EcardApiBusiReportPublic ecardBusiReportPublic) {
        try {
            String salt = getSalt();//盐值
            SysDept dept = sysDeptService.selectSecondLevelDeptByDeptId(ecardBusiReportPublic.getDeptId());
            //根据业务类型id查询业务类型信息
            String configId = ecardBusiReportPublic.getConfigId();
            EcardApiBusiReportConfig reportConfig = ecardBusiReportConfigService.getReportConfigById(Long.valueOf(configId));
            if ("F".equals(reportConfig.getIsMore())){//是否可以多次保存，否的话根据身份证和业务类型id校验是否已存在
                EcardApiBusiReportPublic reportPublic = new EcardApiBusiReportPublic();
                reportPublic.setPubEntName(ecardBusiReportPublic.getPubEntName());//企业名称
                reportPublic.setPubCreditCode(ecardBusiReportPublic.getPubCreditCode());//统一社会信用代码
                reportPublic.setDeptId(dept.getDeptId());
                reportPublic.setConfigId(configId);
                List<EcardApiBusiReportPublic> busiReportPublicList = ecardBusiReportPubliScervice.getMyReportPublicList(reportPublic, salt);
                if (busiReportPublicList.size() > 0){
                    return ResponseModel.error("该客户已存在同类型业务！");
                }
            }
            //管户人id
            ecardBusiReportPublic.setManagementPersonnelId(ecardBusiReportPublic.getCreatorId());
            int id = ecardBusiReportPubliScervice.addReportPublicInfo(ecardBusiReportPublic, salt);
            return id > 0 ? ResponseModel.ok(ecardBusiReportPublic.getId()) : ResponseModel.error("添加失败！");
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }

    /**
     * 电子名片-条件查询对公类业务列表（暂时不用）
     *
     * @author yuzhiguo
     * @date 2024/9/25 下午3:42
     */
    @PostMapping(value = "/getMyReportPublicList")
    @ApiOperation(value = "电子名片-条件查询对公类业务列表", notes = "电子名片-条件查询对公类业务列表")
    @ResponseBody
    @RsaRequest
    public ResponseModel getMyReportPublicList(@RequestBody String json) {
        log.info("接口请求时间-------" + DateUtils.getTime());
        try {
            String salt = getSalt();//盐值
            String currentUserId = getCurrentUserId();
            JSONObject jsonObject = JSONObject.parseObject(json);
            String pubCreditCode = jsonObject.getString("pubCreditCode");//统一社会信用代码
            String pubEntName = jsonObject.getString("pubEntName");//企业名称
            EcardApiBusiReportPublic ecardBusiReportPublic = new EcardApiBusiReportPublic();
            ecardBusiReportPublic.setPubCreditCode(pubCreditCode);
            ecardBusiReportPublic.setPubEntName(pubEntName);
            ecardBusiReportPublic.setManagementPersonnelId(Long.valueOf(currentUserId));
            List<EcardApiBusiReportPublic> busiReportPublicList = ecardBusiReportPubliScervice.getMyReportPublicList(ecardBusiReportPublic, salt);
            log.info("接口返回时间-------" + DateUtils.getTime());
            return ResponseModel.okToJSONArray(busiReportPublicList);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }
}
